﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AutoParkingManagement
{
    public partial class AddDeleteCard : Form
    {
        #region Private members
        private bool _isNew;
        private bool _cardReaderFinished = true;
        #endregion

        #region Properties
        #endregion

        #region Public members
        public delegate void CardAddedRemovedHandler(Guid cardTypeID, string cardID);
        public event CardAddedRemovedHandler CardAddedRemoved;

        public void StopReadCard()
        {
            _cardReaderFinished = true;
            timer1.Stop();
            this.Close();
            this.Dispose();
        }
        #endregion

        #region Events
        private void AddDeleteCard_Load(object sender, EventArgs e)
        {            
            lblCardType.Visible = _isNew;
            cboCardType.Visible = _isNew;            
            if (_isNew)
            {
                this.Text = "Thêm mới thẻ xe";
                this.Height = 180;
                cboCardType.DataSource = Common.GetCardTypes(false);
                cboCardType.DisplayMember = "Name";
                cboCardType.ValueMember = "CardTypeID";
                if (cboCardType.Items.Count>0)
                {
                    cboCardType.SelectedIndex = 0;
                }
            }
            else
            {
                this.Text = "Hủy bỏ thẻ xe";                
                this.Height = 130;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!_cardReaderFinished) return;
            string strCardID = String.Empty;
            if (AppConfig.HasCardReader=="0")
            {                
                strCardID = Guid.NewGuid().ToString(); ;
            }
            else
            {
                _cardReaderFinished = false;
                strCardID = DLL1356.GetCardIDByCardReader(AppConfig.RFID);
                if (String.IsNullOrEmpty(strCardID))
                {
                    MessageBox.Show("Lỗi đọc thẻ.\nXin kiểm tra lại thiết bị đọc thẻ.");
                    timer1.Stop();
                    this.Close();
                    return;
                }
                _cardReaderFinished = true;
            }
            txtCardID.Text = strCardID;            

            if (_isNew)
            {                
                if (cboCardType.Value != null)
                {
                    CardAddedRemoved((Guid)cboCardType.Value, strCardID);
                }
            }
            else
            {                
                CardAddedRemoved(Guid.Empty, txtCardID.Text);
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (!timer1.Enabled)
            {
                timer1.Enabled = true;
                btnStart.Text = "Tạm dừng";                
            }
            else
            {
                timer1.Enabled = false;
                btnStart.Text = "Bắt đầu quét";
            }
            _cardReaderFinished = timer1.Enabled;
            progressBar1.Visible = timer1.Enabled;
        }

        private void AddDeleteCard_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                timer1.Enabled = false;
                this.Close();
                this.Dispose();
            }
        }
        #endregion

        #region Constructros
        public AddDeleteCard(bool isNew)
        {
            InitializeComponent();
            _isNew = isNew;
        }
        #endregion

        

        

        
    }
}
